package com.huas.controller;

import com.huas.common.PageResult;
import com.huas.common.Result;
import com.huas.entity.Role;
import com.huas.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/roleManagement")
public class RoleController {
    @Resource
    private RoleService roleService;
    /**
     * 角色分页列表查询
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('roleManagement:select')")
    public Result<PageResult> page(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10")Integer pageSize){
        log.info("角色列表查询参数：{},{}",page,pageSize);
        PageResult pageResult = roleService.page(page,pageSize);
        return Result.success(pageResult);
    }
    /**
     * 根据角色id查询对应权限集合
     * @param roleId
     * @return
     */
    @GetMapping()
    @PreAuthorize("hasAuthority('roleManagement:select')")
    public Result selectRoleMenu(@RequestParam Integer roleId){
        List<Integer> menus = roleService.selectRoleMenu(roleId);
        return Result.success(menus);
    }
    /**
     * 添加角色
     */
    @PostMapping
    @PreAuthorize("hasAuthority('roleManagement:insert')")
    public Result insert(@RequestBody Role role){
        log.info("添加的角色信息为：{}",role);
        roleService.insert(role);
        return Result.success();
    }
    /**
     * 修改角色
     */
    @PutMapping
    @PreAuthorize("hasAuthority('roleManagement:update')")
    public Result update(@RequestBody Role role){
        log.info("要修改的角色信息为：{}",role);
        roleService.update(role);
        return Result.success();
    }
    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('roleManagement:delete')")
    public Result delete(@PathVariable Integer id){
        log.info("要删除的角色id为：{}",id);
        roleService.delete(id);
        return Result.success();
    }
}
